<?php

/**
 * JingYao-backend
 *
 * @link     https://gitee.com/wang-zhihui-release/jingyao-backend
 * @apiDocument https://gitee.com/wang-zhihui-release/jingyao-backend/wikis/
 */

namespace App\Http\Controllers;

use App\Exceptions\ApiException;
use App\Format\BannerFormat;
use App\Http\Requests\CreateBannerRequest;
use App\Services\BannerService;
use Illuminate\Http\Request;

class BannerController extends Controller
{
    protected $bannerService;

    public function __construct(BannerService $bannerService)
    {
        $this->bannerService = $bannerService;
    }

    public function getBanners()
    {
        $banners = $this->bannerService->getBanners();
        return renderSuccessJson($banners);
    }

    public function getAdminBannerLists(Request $request)
    {
        $page = $request->input('page', 1);
        $pageSize = $request->input('page_size', 10);
        $search = [];
        if ($request->input('title')) {
            $search['title'] = $request->input('title');
        }

        if ($request->input('start_date')) {
            $search['start_date'] = $request->input('start_date');
        }

        if ($request->input('end_date')) {
            $search['end_date'] = $request->input('end_date');
        }

        if ($request->input('identity_visibility')) {
            $search['identity_visibility'] = $request->input('identity_visibility');
        }

        $res = $this->bannerService->getAdminBanners($page, $pageSize, $search);

        return renderSuccessJson($res);
    }

    public function create(CreateBannerRequest $createBannerRequest)
    {
        $format = new BannerFormat($createBannerRequest->all());
        $this->bannerService->create($format);
        return renderSuccessJson();
    }

    public function update(Request $request)
    {
        $format = new BannerFormat($request->all());
        $this->bannerService->update($format);
        return renderSuccessJson();
    }

    public function delete(Request $request)
    {
        $id = $request->input('id');
        if (empty($id)) {
            throw new ApiException('缺少参数', 1);
        }
        $this->bannerService->delete($id);
        return renderSuccessJson();
    }

    public function getXcxPageList()
    {
        return renderSuccessJson(config('xcx_paths'));
    }
}
